<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Zip | CatchAdmin 官网</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.2">
    <style>a[title="站长统计"]{display:none}</style>
    <script data-ad-client="ca-pub-1505209242532150" async="true" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://v1.cnzz.com/z_stat.php?id=1279921342&amp;web_id=1279921342"></script>
    <meta name="description" content="基于 Thinkphp & Vue 开发的后台管理框架">
    <meta name="keywords" content="catchadmin, 后台管理, 前后端分离, thinkphp后台管理框架, thinkphp前后端分离框架,php, elementui">
    <link rel="preload" href="/assets/css/0.styles.6834c255.css" as="style"><link rel="preload" href="/assets/js/app.d0396f44.js" as="script"><link rel="preload" href="/assets/js/2.1cef47df.js" as="script"><link rel="preload" href="/assets/js/33.8610da11.js" as="script"><link rel="prefetch" href="/assets/js/10.483f2013.js"><link rel="prefetch" href="/assets/js/11.981d4461.js"><link rel="prefetch" href="/assets/js/12.95d2c383.js"><link rel="prefetch" href="/assets/js/13.061c2fe0.js"><link rel="prefetch" href="/assets/js/14.ec2753db.js"><link rel="prefetch" href="/assets/js/15.8ba1c321.js"><link rel="prefetch" href="/assets/js/16.f7f0d34c.js"><link rel="prefetch" href="/assets/js/17.aac0bf62.js"><link rel="prefetch" href="/assets/js/18.2e2fe306.js"><link rel="prefetch" href="/assets/js/19.aee304b3.js"><link rel="prefetch" href="/assets/js/20.86a73bc1.js"><link rel="prefetch" href="/assets/js/21.8992c972.js"><link rel="prefetch" href="/assets/js/22.0a78500a.js"><link rel="prefetch" href="/assets/js/23.06342909.js"><link rel="prefetch" href="/assets/js/24.06fd96a3.js"><link rel="prefetch" href="/assets/js/25.00664d4e.js"><link rel="prefetch" href="/assets/js/26.c3d4b548.js"><link rel="prefetch" href="/assets/js/27.3c6927ad.js"><link rel="prefetch" href="/assets/js/28.74498c0b.js"><link rel="prefetch" href="/assets/js/29.aae1a826.js"><link rel="prefetch" href="/assets/js/3.4d2bdca7.js"><link rel="prefetch" href="/assets/js/30.c7732a2c.js"><link rel="prefetch" href="/assets/js/31.20403043.js"><link rel="prefetch" href="/assets/js/32.7b8e9cfe.js"><link rel="prefetch" href="/assets/js/4.d7b1015b.js"><link rel="prefetch" href="/assets/js/5.ff87de2e.js"><link rel="prefetch" href="/assets/js/6.0e91e688.js"><link rel="prefetch" href="/assets/js/7.ba30e213.js"><link rel="prefetch" href="/assets/js/8.35122549.js"><link rel="prefetch" href="/assets/js/9.081b18e5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6834c255.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active no-logo home-link"><!----> <span class="site-name">CatchAdmin 官网</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          🏠首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/docs/">
          📖文档
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item ant-menu-item-selected"><a href="/laravel/" class="router-link-active">
          📖Laravel
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/faq/">
          🤔FAQ
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/donate/">
          🎉赞助名单
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🚀仓库地址
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <!----></nav></div></div> <!----></header> <aside class="sidebar"><div><div class="ads"><div id="ads_1"><img src="https://cover.kancloud.cn/akasishikelu/thinkphp6!middle"> <span title="基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析">基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析</span></div></div> <div role="separator" id="reset-margin" class="ant-divider ant-divider-horizontal ant-divider-dashed"></div></div> <ul class="sidebar-links"><li><a href="/laravel/" aria-current="page" title="介绍" class="sidebar-link">介绍</a></li><li><a href="/laravel/install.html" title="项目安装" class="sidebar-link">项目安装</a></li><li><a href="/laravel/module.html" title="模块" class="sidebar-link">模块</a></li><li><a href="/laravel/model.html" title="模型" class="sidebar-link">模型</a></li><li><a href="/laravel/console.html" title="命令" class="sidebar-link">命令</a></li><li><a href="/laravel/form.html" title="表单" class="sidebar-link">表单</a></li><li><a href="/laravel/table.html" title="表格组件" class="sidebar-link">表格组件</a></li><li><a href="/laravel/macros.html" title="扩展方法" class="sidebar-link">扩展方法</a></li><li><a href="/laravel/excel.html" title="Excel" class="sidebar-link">Excel</a></li><li><a href="/laravel/issue.html" title="常见问题" class="sidebar-link">常见问题</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h1 id="zip"><a href="#zip" class="header-anchor">#</a> Zip</h1> <p><code>CatchAdmin</code> 提供一个 <code>Zip/phar</code> 压缩的功能，主要用于模块的打包和解压，主要为了之后模块的生态作准备，当然开发者有压缩需求也是可以使用的。
可以使用门面 <code>Catcher\Facade\Zipper</code> 操作。</p> <h2 id="简单的例子"><a href="#简单的例子" class="header-anchor">#</a> 简单的例子</h2> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.txt'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="主要的一些方法"><a href="#主要的一些方法" class="header-anchor">#</a> 主要的一些方法</h2> <h3 id="make-pathtofile"><a href="#make-pathtofile" class="header-anchor">#</a> make($pathToFile)</h3> <p>创建或者打开一个 Zip 文件，如果文件不存在的话，他会创建一个新的，并且会返回一个 <code>Zipper</code> 对象用于后续的操作</p> <h3 id="add-files-folder"><a href="#add-files-folder" class="header-anchor">#</a> add($files/folder)</h3> <p>这个方法可以添加文件(数组)或者这个文件夹，如果是文件夹，那么文件夹内的文件会被添加进去。但是这里要注意的是，他并不会添加文件夹到 <code>zip</code> 包内，而仅仅是添加文件夹的文件到包内。</p> <h2 id="addstring-filename-content"><a href="#addstring-filename-content" class="header-anchor">#</a> addString($filename, $content)</h2> <p>通过将名称和内容指定为字符串，将单个文件添加到 zip</p> <h2 id="remove-file-s"><a href="#remove-file-s" class="header-anchor">#</a> remove($file/s)</h2> <p>从 <code>zip</code> 包内移除文件</p> <h2 id="folder-folder"><a href="#folder-folder" class="header-anchor">#</a> folder($folder)</h2> <p>如果需要往 <code>Zip</code> 添加文件夹，并且向文件夹内添加文件，那么就必须使用该方法，下面是一个简单的例子</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">folder</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'index.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>那么 <code>test.zip</code> 在解压之后就会包含一个 <code>test</code> 文件夹以及 <code>test/index.php</code> 文件</p> <h2 id="listfiles-regexfilter-null"><a href="#listfiles-regexfilter-null" class="header-anchor">#</a> listFiles($regexFilter = null)</h2> <p>列出 <code>zip</code> 内的所有文件，那然也可以使用正则过滤，比如列出 <code>zip</code> 内的所有 <code>.php</code> 文件</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">listFiles</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/\.php$/i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="close"><a href="#close" class="header-anchor">#</a> close()</h2> <p>使用结束后调用，关闭资源</p> <h2 id="extractto-path"><a href="#extractto-path" class="header-anchor">#</a> extractTo($path)</h2> <p>将 Zip 的内容解压到指定的位置，简单的案例,解压到当前目录</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">extractTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'./'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>当然也可以选择 <code>Zip</code> 内的某一个文件夹。例如</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">folder</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">extractTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'./'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="extractmatchingregex-path-regex"><a href="#extractmatchingregex-path-regex" class="header-anchor">#</a> extractMatchingRegex($path, $regex)</h2> <p>可以使用正则过滤需要的文件，在解压。例如：只需要解压 <code>.php</code> 文件</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.zip'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">folder</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">extractMatchingRegex</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'./'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/\.php$/i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="模块打包完成代码"><a href="#模块打包完成代码" class="header-anchor">#</a> 模块打包完成代码</h2> <p>最后来看下，<code>CatchAdmin</code> 模块打包的完整代码，非常简单。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 模块的路径</span>
<span class="token variable">$modulePath</span> <span class="token operator">=</span> <span class="token variable">$path</span> <span class="token operator">?</span> <span class="token variable">$path</span> <span class="token operator">.</span> <span class="token constant">DIRECTORY_SEPARATOR</span> <span class="token operator">.</span> <span class="token variable">$moduleName</span> <span class="token punctuation">:</span> <span class="token class-name static-context">CatchAdmin</span><span class="token operator">::</span><span class="token function">getModulePath</span><span class="token punctuation">(</span><span class="token variable">$moduleName</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 默认 zip 将打包在 storage 目录下</span>
<span class="token variable">$zipper</span> <span class="token operator">=</span>  <span class="token class-name static-context">Zipper</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token constant">DIRECTORY_SEPARATOR</span> <span class="token operator">.</span> <span class="token variable">$moduleName</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.zip'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">allFiles</span><span class="token punctuation">(</span><span class="token variable">$modulePath</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$file</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$file</span><span class="token operator">-&gt;</span><span class="token function">getRelativePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 将目录添加到 zip 内，并且添加文件</span>
        <span class="token variable">$zipper</span><span class="token operator">-&gt;</span><span class="token function">folder</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token operator">-&gt;</span><span class="token function">getRelativePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token operator">-&gt;</span><span class="token function">getPathname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 这是添加根目录下文件</span>
        <span class="token variable">$zipper</span><span class="token operator">-&gt;</span><span class="token function">folder</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token operator">-&gt;</span><span class="token function">getPathname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token variable">$zipper</span><span class="token operator">-&gt;</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/catch-admin/document/edit/master/docs/laravel/zip.md" target="_blank" rel="noopener noreferrer">在 Github 编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <!----> </main> <!----></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.d0396f44.js" defer></script><script src="/assets/js/2.1cef47df.js" defer></script><script src="/assets/js/33.8610da11.js" defer></script>
  </body>
</html>